package com.gw.vip.common.util;

import com.gw.vip.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author GW
 * @date 2022-01-08
 * @description
 */
@Slf4j
public class DateTools {
    public static String time2str14(LocalDateTime time){
        if (time==null){
            return null;
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        return time.format(df);
    }

    public static String time2str19(LocalDateTime time){
        if (time==null){
            return null;
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return time.format(df);
    }

    public static String time2str8(LocalDateTime time){
        if (time==null){
            return null;
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        return time.format(df);
    }

    public static String date2str8(LocalDate date){
        if (date==null){
            return null;
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        return date.format(df);
    }

    public static String date2str10(LocalDate date){
        if (date==null){
            return null;
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return date.format(df);
    }

    public static LocalDateTime str2time14(String time){
        if (StringUtils.isBlank(time)){
            return null;
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        return LocalDateTime.parse(time, df);
    }
}
